﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MyServer.TcpIP;
using MyServer.Common;
using System.Net;
using System.Net.Sockets;

namespace ServerUI
{
    public partial class TcpIp : Form
    {
        public static TcpIpCenter Center;

        public TcpIp()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IPAddress addr = IPAddress.Parse(textBox1.Text);
            IPEndPoint endp = new IPEndPoint(addr, (int)numericUpDown1.Value);
            Center = new TcpIpCenter(endp);
            Center.Start();
            Center.ConnectionManager.NewConnectionJoin += ConnectionManager_NewConnectionJoin;
            Center.Reader.AfterReadData += Reader_AfterReadData;
            timer1.Start();
        }
        private delegate void reader_a_d(object sender, ref DataAfterTransportEventArgs e);
        void Reader_AfterReadData(object sender, ref MyServer.Common.DataAfterTransportEventArgs e)
        {
            if (treeView1.InvokeRequired)
            {
                reader_a_d de = new reader_a_d(Reader_AfterReadData);
                treeView1.Invoke(de, sender, e);
            }
            else
            {
                DataPackageCollection pkg = e.OperateData;
                if (pkg.State==PackageCollectionState.ReciveCompelete)
                {
                    string s = Encoding.UTF8.GetString(DataPacker.UnPackData(pkg));
                    TreeNode tnode = new TreeNode();
                    tnode.Text = ((TcpClient)pkg.Tag).Client.RemoteEndPoint.ToString();
                    tnode.Tag = s;
                    treeView1.Nodes.Add(tnode);
                }
            }
        }

        private delegate void ncondel(object sender, ref MyServer.Common.ConnectEventArgs e);
        void ConnectionManager_NewConnectionJoin(object sender, ref MyServer.Common.ConnectEventArgs e)
        {
            if (listBox1.InvokeRequired)
            {
                ncondel de = new ncondel(ConnectionManager_NewConnectionJoin);
                listBox1.Invoke(de, sender, e);
            }
            else
            {
                TcpClient c = (TcpClient)e.Connect;
                listBox1.Items.Add(c.Client.RemoteEndPoint.ToString());
            }


        }

        private void button2_Click(object sender, EventArgs e)
        {
            Center.Stop();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.numericUpDown1.Minimum = IPEndPoint.MinPort;
            this.numericUpDown1.Maximum = IPEndPoint.MaxPort;
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                this.textBox2.Text = (String)treeView1.SelectedNode.Tag;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.toolStripStatusLabel1.Text = "连接数：" + Center.ConnectionManager.ClientList.Count +
                                            "，监听数：" + Center.Listener.GetConnectCount() +
                                            "，入队列数：" + Center.Reader.ReadQueue.Count +
                                            "，入队列未分组数据包数："+Center.Reader.UnGroupedPkg.Count +
                                            "，出队列数：" + Center.Sender.SendQueue.Count +
                                            "，操作队列数：" + Center.Operator.OperateQueue.Count+
                                            "，已接收数据包数：" + treeView1.Nodes.Count;
        }
    }
}
